/**
 * Domains 模块 API
 * 自定义域名相关接口（Beta）
 */

import { request, type ApiResponse } from "../request";

// ============ 类型定义 ============

/**
 * 自定义主机名状态
 */
export type CustomHostnameStatus =
	| "1_not_started"
	| "2_initiated"
	| "3_challenge_verified"
	| "4_origin_setup_completed"
	| "5_services_reconfigured";

/**
 * SSL 验证记录
 */
export interface SslValidationRecord {
	txt_name: string;
	txt_value: string;
}

/**
 * SSL 验证错误
 */
export interface SslValidationError {
	message: string;
}

/**
 * SSL 信息
 */
export interface SslInfo {
	status: string;
	validation_records: SslValidationRecord[];
	validation_errors?: SslValidationError[];
}

/**
 * 所有权验证信息
 */
export interface OwnershipVerification {
	type: string;
	name: string;
	value: string;
}

/**
 * 自定义主机名结果
 */
export interface CustomHostnameResult {
	id: string;
	hostname: string;
	ssl: SslInfo;
	ownership_verification: OwnershipVerification;
	custom_origin_server: string;
	verification_errors?: string[];
	status: string;
}

/**
 * 自定义主机名数据
 */
export interface CustomHostnameData {
	success: boolean;
	errors: unknown[];
	messages: unknown[];
	result: CustomHostnameResult;
}

/**
 * 自定义主机名响应
 */
export interface CustomHostnameResponse {
	status: CustomHostnameStatus;
	custom_hostname: string;
	data: CustomHostnameData;
}

/**
 * 更新自定义主机名请求体
 */
export interface UpdateCustomHostnameBody {
	custom_hostname: string;
}

/**
 * Vanity 子域名状态
 */
export type VanitySubdomainStatus = "not-used" | "custom-domain-used" | "active";

/**
 * Vanity 子域名配置响应
 */
export interface VanitySubdomainConfigResponse {
	status: VanitySubdomainStatus;
	custom_domain?: string;
}

/**
 * Vanity 子域名请求体
 */
export interface VanitySubdomainBody {
	vanity_subdomain: string;
}

/**
 * 子域名可用性响应
 */
export interface SubdomainAvailabilityResponse {
	available: boolean;
}

/**
 * 激活 Vanity 子域名响应
 */
export interface ActivateVanitySubdomainResponse {
	custom_domain: string;
}

// ============ API 方法 ============

/**
 * 获取项目自定义主机名配置（Beta）
 */
export function getCustomHostnameConfig(
	ref: string
): Promise<ApiResponse<CustomHostnameResponse>> {
	return request.get<CustomHostnameResponse>(
		`/v1/projects/${ref}/custom-hostname`
	);
}

/**
 * 删除项目自定义主机名配置（Beta）
 */
export function deleteCustomHostnameConfig(
	ref: string
): Promise<ApiResponse<Record<string, unknown>>> {
	return request.delete<Record<string, unknown>>(
		`/v1/projects/${ref}/custom-hostname`
	);
}

/**
 * 初始化项目自定义主机名配置（Beta）
 */
export function initializeCustomHostname(
	ref: string,
	data: UpdateCustomHostnameBody
): Promise<ApiResponse<CustomHostnameResponse>> {
	return request.post<CustomHostnameResponse>(
		`/v1/projects/${ref}/custom-hostname/initialize`,
		data
	);
}

/**
 * 验证项目自定义主机名的 DNS 配置（Beta）
 */
export function reverifyCustomHostname(
	ref: string
): Promise<ApiResponse<CustomHostnameResponse>> {
	return request.post<CustomHostnameResponse>(
		`/v1/projects/${ref}/custom-hostname/reverify`
	);
}

/**
 * 激活项目自定义主机名（Beta）
 */
export function activateCustomHostname(
	ref: string
): Promise<ApiResponse<CustomHostnameResponse>> {
	return request.post<CustomHostnameResponse>(
		`/v1/projects/${ref}/custom-hostname/activate`
	);
}

/**
 * 获取当前 Vanity 子域名配置（Beta）
 */
export function getVanitySubdomainConfig(
	ref: string
): Promise<ApiResponse<VanitySubdomainConfigResponse>> {
	return request.get<VanitySubdomainConfigResponse>(
		`/v1/projects/${ref}/vanity-subdomain`
	);
}

/**
 * 删除项目 Vanity 子域名配置（Beta）
 */
export function deleteVanitySubdomainConfig(
	ref: string
): Promise<ApiResponse<Record<string, unknown>>> {
	return request.delete<Record<string, unknown>>(
		`/v1/projects/${ref}/vanity-subdomain`
	);
}

/**
 * 检查 Vanity 子域名可用性（Beta）
 */
export function checkVanitySubdomainAvailability(
	ref: string,
	data: VanitySubdomainBody
): Promise<ApiResponse<SubdomainAvailabilityResponse>> {
	return request.post<SubdomainAvailabilityResponse>(
		`/v1/projects/${ref}/vanity-subdomain/check-availability`,
		data
	);
}

/**
 * 激活 Vanity 子域名（Beta）
 */
export function activateVanitySubdomain(
	ref: string,
	data: VanitySubdomainBody
): Promise<ApiResponse<ActivateVanitySubdomainResponse>> {
	return request.post<ActivateVanitySubdomainResponse>(
		`/v1/projects/${ref}/vanity-subdomain/activate`,
		data
	);
}

export default {
	getCustomHostnameConfig,
	deleteCustomHostnameConfig,
	initializeCustomHostname,
	reverifyCustomHostname,
	activateCustomHostname,
	getVanitySubdomainConfig,
	deleteVanitySubdomainConfig,
	checkVanitySubdomainAvailability,
	activateVanitySubdomain
};
